dataclasses.replace: fall through to typeshed sig#15962
Merged
hauntsaninja merged 3 commits intopython:masterfrom Aug 29, 2023
Merged
dataclasses.replace: fall through to typeshed sig#15962hauntsaninja merged 3 commits intopython:masterfrom
hauntsaninja merged 3 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
e91d2da to
a2f3780
Compare
a2f3780 to
a657d10
Compare
This comment has been minimized.
This comment has been minimized.
6fd543b to
0b95f3e
Compare
This comment has been minimized.
This comment has been minimized.
sobolevn
reviewed
Aug 27, 2023
Member
sobolevn
left a comment
There was a problem hiding this comment.
Generally looks good, just one question :)
|
|
||
| p = Person('John') | ||
| y = replace(p, name='Bob') # E: Argument 1 to "replace" has incompatible type "Person"; expected a dataclass | ||
| y = replace(p, name='Bob') |
Member
There was a problem hiding this comment.
I think that this can potentially introduce some new weak spots, but on other hand it can eventually work :)
Contributor
Author
There was a problem hiding this comment.
This is a false positive but it's due to #15974.
| # error before type-guard | ||
| y = replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "object" | ||
| # no error after type-guard | ||
| if is_dataclass(x) and not isinstance(x, type): |
Member
There was a problem hiding this comment.
Do we need to test just if is_dataclass(x):?
Contributor
Author
There was a problem hiding this comment.
It's because type is an object, and is_dataclass on an object either means it's a dataclass instance or type.
I'll improve the test to clarify that.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: vision (https://github.com/pytorch/vision): typechecking got 1.37x slower (36.8s -> 50.5s)
(Performance measurements are based on a single noisy sample)
|
sobolevn
approved these changes
Aug 28, 2023
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If the dataclasses plugin cannot determine a signature for
dataclasses.replace, it should not report an error. The underlying typeshed signature will get a shot at verifying the type and reporting an error, and it would enable the following pattern (without typingreplace's kwargs, though):